fun Number_Divisors(n) = 
  let
    fun f(d, n, tot) = 
	  if d*d > n then tot
	  else 
	    if d*d = n then f(d+1, n, tot+1)
		else
		  if n mod d = 0 then f(d+1, n, tot+2)
		  else f(d+1, n, tot);
  in 
    f(1, n, 0)
  end;
  
fun Output(m, i) = 
  if Number_Divisors(m) > 500 then m
  else Output(m+i, i+1);
  
Output(0, 1);